﻿package com.hnev.gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import com.hnev.SystemData.SQL;


public class Login extends JFrame{
	
	private JPasswordField passwd;
	private JTextField textField;
	private JLabel label1,label2,label3;
	private JButton button;
	
	public Login(){
		label1 = new JLabel("用户名:");
		label2 = new JLabel("密     码:");
		label3 = new JLabel();
		passwd = new JPasswordField();
		textField = new JTextField();
		button = new JButton("登录");
		setLayout(null);
		label1.setBounds(90, 55, 50, 30);
		label2.setBounds(90,130,50,30);
		label3.setBounds(180,170,160,30);
		textField.setBounds(140,55,200,30);
		passwd.setBounds(140,130,200,30);
		button.setBounds(185, 220, 110, 45);
		add(label1);
		add(label2);
		add(label3);
		add(passwd);
		add(textField);
		add(button);	
		setLocation(520, 300);
		setSize(470, 400);
		setVisible(true);
		
		//设置与数据库的连接
		final SQL sql = new SQL();
		button.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				System.out.println("点击一次");//调试信息
				try {
					sql.getConnection();
					String Id = textField.getText();
					String Passwd = new String(passwd.getPassword());
					String sqlword = "select * from user where id = '"+Id+"'";
					ResultSet st = sql.executeQuery(sqlword, null);
					//判断登录
					if(st.next()){
						System.out.println(st.getString(1));
						if(st.getString(1) != null){
							if(st.getString(2).equals(Passwd)){
								System.out.println("登录成功");
								label3.setText("登陆成功");
								new Thread(new Runnable() {
									
									@Override
									public void run() {
										try {
											Thread.sleep(1500);
											new AdminSystem();
											dispose();
										} catch (InterruptedException e) {
											// TODO Auto-generated catch block
											e.printStackTrace();
										}
										
									}
								}).start();
							}else{
								System.out.println("密码错误");
								label3.setText("密码错误");
								new Thread(new Runnable() {
									
									@Override
									public void run() {
										try {
											Thread.sleep(1500);
											label3.setText(null);
										} catch (InterruptedException e) {
											// TODO Auto-generated catch block
											e.printStackTrace();
										}
										
									}
								}).start();
							}
						}
					}else{
						System.out.println("帐号未找到或帐号错误");
						label3.setText("帐号未找到或帐号错误");
						new Thread(new Runnable() {
							
							@Override
							public void run() {
								try {
									Thread.sleep(1500);
									label3.setText(null);
								} catch (InterruptedException e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								}
								
							}
						}).start();
					}
					
					
				
				} catch ( SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} 
				
			}
		});
	}
}
